package com.jldt.flutter_douyin.douyinapi;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.bytedance.sdk.open.aweme.CommonConstants;
import com.bytedance.sdk.open.aweme.authorize.model.Authorization;
import com.bytedance.sdk.open.aweme.common.handler.IApiEventHandler;
import com.bytedance.sdk.open.aweme.common.model.BaseReq;
import com.bytedance.sdk.open.aweme.common.model.BaseResp;
import com.bytedance.sdk.open.aweme.share.Share;
import com.bytedance.sdk.open.douyin.DouYinOpenApiFactory;
import com.bytedance.sdk.open.douyin.api.DouYinOpenApi;
import com.jldt.flutter_douyin.FlutterEventChannel;
import com.jldt.flutter_douyin.FlutterMethodChannel;

import java.util.HashMap;
import java.util.Map;

import io.flutter.Log;
import io.flutter.embedding.android.FlutterActivity;

public class DouYinEntryActivity extends FlutterActivity implements IApiEventHandler {

    DouYinOpenApi douYinOpenApi;
    FlutterEventChannel eventChannel;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        eventChannel = FlutterEventChannel.getInstance();
        douYinOpenApi = DouYinOpenApiFactory.create(this);
        douYinOpenApi.handleIntent(getIntent(), this);
    }

    @Override
    public void onReq(BaseReq req) {

    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == CommonConstants.ModeType.SHARE_CONTENT_TO_TT_RESP) {
            Share.Response response = (Share.Response) resp;

            Map<String, String> args = new HashMap<>();
            args.put("code", response.errorMsg);
            args.put("msg", response.errorMsg);
            eventChannel.sendEventData(args);
            //Toast.makeText(this, " code：" + response.errorCode + " 文案：" + response.errorMsg, Toast.LENGTH_SHORT).show();
        } else if (resp.getType() == CommonConstants.ModeType.SEND_AUTH_RESPONSE) {
            Authorization.Response response = (Authorization.Response) resp;
            Intent intent = null;
            if (resp.isSuccess()) {
                Map<String, String> args = new HashMap<>();
                args.put("code", response.authCode);
                args.put("msg", response.grantedPermissions);
                eventChannel.sendEventData(args);
                //Toast.makeText(this, "授权成功，获得权限：" + response.grantedPermissions,Toast.LENGTH_LONG).show()
            }
        }
        finish();

    }

    @Override
    public void onErrorIntent(Intent intent) {
        // 错误数据
        Toast.makeText(this, "Intent出错", Toast.LENGTH_LONG).show();
    }
}

